# --**coding:utf-8**--
# Create by author: hemq

# 进行母包／渠道包存储操作，存放历史版本

import channelconfig, os
from channel.python import Util
from channel.settings import filetool
from channel.models import HistoryChannelapk


class apkManage:

    def __init__(self, channekApkName):
        # 母包地址
        self.baseApkpath = filetool.getBaseApkPath()[0]
        # 母包版本
        self.apkVersion = filetool.getVersionFromFile()
        self.channelApkName = channekApkName
        self.Money = '¥'

    def baseApkManage(self):  # 历史版本母包管理

        # 创建对应版本母包文件夹
        baseApkFolder = './channel/staticc/baseApkFolder/'
        if not os.path.exists(baseApkFolder):
            os.mkdir(baseApkFolder)
        baseApkArchive = baseApkFolder+self.apkVersion+'/'

        if not os.path.exists(baseApkArchive):
            os.mkdir(baseApkArchive)

        # copy baseapk/apk --> baseApkFolder 实现历史版本母包存储
        Util.copyFile(self.baseApkpath, baseApkArchive)

    def channelApkMange(self):  # 历史渠道包管理

        channelApkPath = './channel/staticc/channelApkFolder/'
        if not os.path.exists(channelApkPath):
            os.mkdir(channelApkPath)
        channelApkArchive = channelApkPath + self.apkVersion + '/'

        if not os.path.exists(channelApkArchive):
            os.mkdir(channelApkArchive)
        # channelapkName:视图函数构建完渠道包后传入函数

        srcChannelApk = './channel/staticc/channelApk'
        # get all apk name --> os.listdir()
        channelDirList = os.listdir(srcChannelApk)
        for i in range(0, len(channelDirList)):
            path = os.path.join(srcChannelApk, channelDirList[i])
            if os.path.isfile(path):
                Util.copyFile(path, channelApkArchive)

    def baseApkDelete(self):

        # baseapk目录只存储当次构建上传的母包，每次构建完毕删除当次上传母包
        try:
            os.remove(self.baseApkpath)
        except Exception as error:

            print 'delete fail'